package com.sls.web.manager.action.recharge;

import java.util.List;

import javax.annotation.Resource;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.Supplier;
import com.sls.web.manager.recharge.pojo.SupplierAccount;
import com.sls.web.manager.recharge.querybean.SupplierAccountQuery;
import com.sls.web.manager.recharge.service.SupplierAccountService;

public class SupplierAccountAction extends SuperAction {
	private static final long serialVersionUID = 4361068469185173283L;
	@Resource(name = "supplierAccountService")
	private SupplierAccountService supplierAccountService;
	/**
	 * 返回分页对象
	 */
	public Pagination<?> page;
	private SupplierAccountQuery supplierAccountQuery;
	private SupplierAccount supplierAccount;
	private String id;
	private List<Supplier> supplierList;

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public SupplierAccountQuery getSupplierAccountQuery() {
		return supplierAccountQuery;
	}

	public void setSupplierAccountQuery(
			SupplierAccountQuery supplierAccountQuery) {
		this.supplierAccountQuery = supplierAccountQuery;
	}

	public SupplierAccount getSupplierAccount() {
		return supplierAccount;
	}

	public void setSupplierAccount(SupplierAccount supplierAccount) {
		this.supplierAccount = supplierAccount;
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	// 添加
	public String add() {
		supplierAccount = new SupplierAccount();
		supplierList=supplierAccountService.querySupplier(this.getCurrentOperator());
		return "edit";
	}

	// 添加供货商提供的接口账号（工号）
	public String addSupplierAccount() throws Exception {
		try {
			if (supplierAccount.getEveryDayQuota() != null)
				supplierAccount.setEveryDayQuota(supplierAccount
						.getEveryDayQuota() * 1000);
			supplierAccountService.addSupplierAccount(supplierAccount,
					this.getCurrentOperator());
			redirectionUrl = "supplier_account!list.htm";
			return "success";
		} catch (ManagerException e) {
			supplierAccount = null;
			addActionError(e.getMessage());
			return this.add();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.add();
		}

	}

	// 列表
	public String list() {
		if (supplierAccountQuery == null) {
			supplierAccountQuery = new SupplierAccountQuery();
			supplierAccountQuery.setPageSize(Constants.INIT_PAGESIZE);
			supplierAccountQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = supplierAccountService.querySupplierAccount(
					supplierAccountQuery, getCurrentOperator());
		} catch (Exception e) {
			return ERROR;
		}

		return "list";
	}

	// 编辑
	public String edit() throws Exception {
		if (id != null) {
			supplierAccount = supplierAccountService.querySupplierAccountById(
					id, this.getCurrentOperator());
			supplierList=supplierAccountService.querySupplier(this.getCurrentOperator());
		}
		return "edit";
	}

	// 编辑供货商提供的接口账号（工号）
	public String editSupplierAccount() throws Exception {
		if (supplierAccount.getEveryDayQuota() != null)
			supplierAccount
					.setEveryDayQuota(supplierAccount.getEveryDayQuota() * 1000);
		boolean result = supplierAccountService.updateSupplierAccount(
				supplierAccount, this.getCurrentOperator());
		if (!result) {
			addActionError("修改失败!");
			return this.edit();
		} else {
			redirectionUrl = "supplier_account!list.htm";
			return "success";
		}
	}

	// 删除
	public String delete() throws Exception {
		try{
			supplierAccountService.delete(id,getCurrentOperator());
			redirectionUrl = "supplier_account!list.htm";
			return "success";
		}catch (ManagerException e){
			addActionError(e.getMessage());
			return this.list();
		}
	}

}